#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date    : 2014-08-22 01:14:23
# @Author  : shitao.tommy (hero007asd@gmail.com)
# @Link    : http://example.org
# @Version : $Id$

from web.handler.api import BaseApiHandler
from lib.tornado_routes import route
from tornado import gen
from lib import libimage,libhttp
from tornado.web import stream_request_body

head_title = '登陆接口'

@route(r'api/myapi',name='apitest2')
class TestApi1(BaseApiHandler):
    '''
    @title 测试用的登陆接口
    @method get
    @param username 姓名 12位最长
    @param password 密码 md5加密
    @return {return_code:1, message:null}
    @returnfield status 登陆状态 1.登陆成功2.登陆失败
    @description test api for description
    '''
    @gen.coroutine
    def get(self):
        result = [{'a':"sdf",'b':"basdf"},{'a':"sdf",'b':"basdf"}]
        result = "是他"
        # import json
        # result = json.loads(result)
        # print result
        self.write_success(result)
        self.finish()

@route(r'api/img')
class ImageApi(BaseApiHandler):

    @gen.coroutine
    def get(self):
        libimage.resizeImg(ori_img='/opt/web/heaven-api/angle/1.jpg',dst_img='2.jpg',dst_w=200,dst_h=200)
        self.finish()


@route(r'api/httpclient')
class ClientApi(BaseApiHandler):

    @gen.coroutine
    def get(self):
        a = libhttp.get_url_body('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=116.226.44.139')
        # print a
        self.finish()

@route(r'api/upload/img')
@stream_request_body 
class StreamHandler(BaseApiHandler):
    def get(self):
        self.write('''<html><body>
<form method="POST" action="/api/upload/img" enctype="multipart/form-data">
File #1: <input name="file1" type="file"><br>
File #2: <input name="file2" type="file"><br>
File #3: <input name="file3" type="file"><br>
Other field 1: <input name="other1" type="text"><br>
Other field 2: <input name="other2" type="text"><br>
Other field 3: <input name="other3" type="text"><br>
<input type="submit">
</form>
</body></html>''')

    def post(self):
        print dir(self.request.files)
        # try:
        #     print dir(self)
        #     #self.fout.close()
        #     self.ps.finish_receive()
        #     # Use parts here!
        #     self.set_header("Content-Type","text/plain")
        #     oout = sys.stdout
        #     try:
        #         sys.stdout = self
        #         self.ps.examine()
        #     finally:
        #         sys.stdout = oout
        # finally:
        #     # Don't forget to release temporary files.
        #     self.ps.release_parts()

    def prepare(self):
        # TODO: get content length here?
        try:
            total = int(self.request.headers.get("Content-Length","0"))
        except:
            total = 0
        # self.ps = MyPostDataStreamer(total) #,tmpdir="/tmp"
        #self.fout = open("raw_received.dat","wb+")

    def data_received(self, chunk):
        #self.fout.write(chunk)
        print len(chunk)
        # self.ps.receive(chunk)

# from post_streamer import PostDataStreamer
# class MyPostDataStreamer(PostDataStreamer):
#     percent = 0

#     def on_progress(self):
#         """Override this function to handle progress of receiving data."""
#         if self.total:
#             new_percent = self.received*100//self.total
#             if new_percent != self.percent:
#                 self.percent = new_percent
#                 print("progress",new_percent)


